package com.example.oss.util;

public class RedisKeyUtil {

    private static final String SPLIT = ":";  //key的分隔符
    private static final String LINK = "-";  //key的连接符
    private static final String COURSE_AMOUNT_KEY = "course-amount"; //课程报名余额

    private static final String ACCESS_LIMIT_KEY = "access-limit";  //接口限流
    private static final String REFRESH_LIMIT_KEY = "refresh-limit"; //接口防刷
    private static final String USER_INFO_KEY = "user-info"; //用户信息
    private static final String ORDER_KEY = "course-order";  //选课订单

    private static final String LOGIN_LIMIT_KEY = "login-limit"; //限制登录点击次数

    //缓存的课程列表（a为主，b为副，读的时候先读A, A没有再读B，更新时先更新B，防止缓存击穿）
    private static final String COURSE_LIST_ABLE_A_KEY = "course-list-able-a";
    private static final String COURSE_LIST_ABLE_B_KEY = "course-list-able-b";
    private static final String COURSE_LIST_ALL_A_KEY = "course-list-all-a";
    private static final String COURSE_LIST_ALL_B_KEY = "course-list-all-b";

    private static final String IS_EMPTY_KEY = "is-empty"; //是否为空


    public static String getLoginLimitKey(String ip, String uri) {
        return LOGIN_LIMIT_KEY + LINK + ip + LINK + uri;
    }

    public static String getIsEmptyKey(int courseId) {
        return IS_EMPTY_KEY + SPLIT + courseId;
    }

    public static String getCourseListAbleAKey() {
        return COURSE_LIST_ABLE_A_KEY;
    }

    public static String getCourseListAbleBKey() {
        return COURSE_LIST_ABLE_B_KEY;
    }

    public static String getCourseListAllAKey() {
        return COURSE_LIST_ALL_A_KEY;
    }

    public static String getCourseListAllBKey() {
        return COURSE_LIST_ALL_B_KEY;
    }




    public static String getRefreshLimitKey(String uri) {
        return REFRESH_LIMIT_KEY + SPLIT + uri;
    }

    /**
     * 获取每个课程的课程剩余可报人数的key
     *
     * @param courseId
     * @return
     */
    public static String getCourseAmountKey(int courseId) {
        return COURSE_AMOUNT_KEY + SPLIT + courseId;
    }

    /**
     * 获取课程订单的key
     */
    public static String getOrderKey(int stuId) {
        return ORDER_KEY + SPLIT + stuId;
    }


    public static String getAccessLimitKey(String uri, int stuId) {
        return ACCESS_LIMIT_KEY + LINK + uri + LINK + stuId;
    }

    public static String getUserInfoKey(String username) {
        return USER_INFO_KEY + LINK + username;
    }

}
